<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Fetch an SNMP object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="snmp.construct.html">« SNMP::__construct</a></li>
      <li style="float: right;"><a href="snmp.geterrno.html">SNMP::getErrno »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.snmp.html">SNMP</a></li>
    <li>Fetch an SNMP object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="snmp.get" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SNMP::get</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SNMP::get</span> &mdash; <span class="dc-title">Fetch an <abbr title="Simple Network Management Protocol">SNMP</abbr> object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-snmp.get-description">
  <h3 class="title">说明</h3>

  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SNMP::get</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">string</span></span> <code class="parameter">$objectId</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserveKeys</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Fetch an <abbr title="Simple Network Management Protocol">SNMP</abbr> object specified in
   <code class="parameter">objectId</code> using GET query.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-snmp.get-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
  If <code class="parameter">objectId</code> is a string, then <span class="methodname"><strong>SNMP::get()</strong></span>
  will return <abbr title="Simple Network Management Protocol">SNMP</abbr> object as string. If
  <code class="parameter">objectId</code> is a array, all requested <abbr title="Simple Network Management Protocol">SNMP</abbr>
  objects will be returned as associative array of the <abbr title="Simple Network Management Protocol">SNMP</abbr> object ids and their
  values. 
  </p>
  <p class="para">
  <dl>
   
    <dt>
<code class="parameter">objectId</code></dt>

    <dd>

     <p class="para">
      The <abbr title="Simple Network Management Protocol">SNMP</abbr> object (OID) or objects
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">preserveKeys</code></dt>

    <dd>

     <p class="para">
      When <code class="parameter">objectId</code> is a array and
      <code class="parameter">preserveKeys</code> set to <strong><code>true</code></strong> keys in results
      will be taken exactly as in <code class="parameter">objectId</code>,
      otherwise <var class="varname">SNMP::oid_output_format</var> property is used to determinate
      the form of keys.
     </p>
    </dd>

   
  </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-snmp.get-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <abbr title="Simple Network Management Protocol">SNMP</abbr> objects requested as string or array
   depending on <code class="parameter">objectId</code> type or <strong><code>false</code></strong> on error.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-snmp.get-errors">
<h3 class="title">错误／异常</h3>
<p class="para">
 本方法默认不抛出任何异常。如果要在某些库出错时抛出 SNMPException 
 异常，用户需要将 SNMP 类参数 <code class="parameter">exceptions_enabled</code> 
 设定为相应的值。更多细节见
 <a href="class.snmp.html#snmp.props.exceptions-enabled" class="link">
  <code class="parameter">SNMP::$exceptions_enabled</code> 的解释</a>。
</p>
</div>



 <div class="refsect1 examples" id="refsect1-snmp.get-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="snmp.get.example.singleoid">
   <p><strong>示例 #1 Single <abbr title="Simple Network Management Protocol">SNMP</abbr> object</strong></p>
   <div class="example-contents"><p>
     Single <abbr title="Simple Network Management Protocol">SNMP</abbr> object may be requested in two ways: as
     string resulting string return value or as single-element array with
     associative array as output.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$session&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">(</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">::</span><span style="color: #0000BB">VERSION_1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"public"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$sysdescr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">"sysDescr.0"</span><span style="color: #007700">);<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$sysdescr</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$sysdescr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"sysDescr.0"</span><span style="color: #007700">));<br />&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sysdescr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
STRING: Test server
Array
(
    [SNMPv2-MIB::sysDescr.0] =&gt; STRING: Test server
)
</pre></div>
   </div>
  </div>
  <div class="example" id="snmp.get.example.oidarray">
   <p><strong>示例 #2 Multiple <abbr title="Simple Network Management Protocol">SNMP</abbr> objects</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
$session&nbsp;=&nbsp;new&nbsp;SNMP(SNMP::VERSION_1,&nbsp;"127.0.0.1",&nbsp;"public");<br />&nbsp;&nbsp;$results&nbsp;=&nbsp;$session-&gt;get(array("sysDescr.0",&nbsp;"sysName.0"));<br />&nbsp;&nbsp;print_r($results);<br />&nbsp;&nbsp;$session-&gt;close();</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [SNMPv2-MIB::sysDescr.0] =&gt; STRING: Test server
    [SNMPv2-MIB::sysName.0] =&gt; STRING: myhost.nodomain
)
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-snmp.get-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="methodname"><a href="snmp.geterrno.html" class="methodname" rel="rdfs-seeAlso">SNMP::getErrno()</a> - Get last error code</span></li>
   <li class="member"><span class="methodname"><a href="snmp.geterror.html" class="methodname" rel="rdfs-seeAlso">SNMP::getError()</a> - Get last error message</span></li>
  </ul>
 </div>


</div></div></div></body></html>